﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using TalksLog.Domain.Core;

namespace TalksLog.Domain.Account.Entities
{
    /// <summary>
    /// 邮箱验证。流程：1，生成VerificationCode并发送邮件；2，进入邮箱点击连接；3，验证VerificationCode
    /// </summary>
    public class EmailVerification:EntityBase, IAggregateRoot
    {
        private EmailVerification() { }

        public string Email { get; set; }

        public string VerificationCode { get; set; }

        public DateTime CreateTime { get; set; }

        public DateTime? VerificationTime{ get; set; }

        internal static EmailVerification Create(string email)
        {
            if (string.IsNullOrWhiteSpace(email))
            {
                throw new ArgumentException("请输入邮箱");
            }
            if (!Regex.IsMatch(email, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
            {
                throw new ArgumentException("邮箱格式不正确");
            }

            EmailVerification newEmailVerification = new EmailVerification();
            newEmailVerification.CreateTime = DateTime.Now;
            newEmailVerification.Email = email;
            newEmailVerification.VerificationCode = Guid.NewGuid().ToString().Replace("-", "");

            return newEmailVerification;
        }
    }
}
